package com.cooper.android.comm;

import com.cooper.android.comm.DownAppInfoGetTask.AppVersionInfo;
import com.cooper.scraper.R;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class VerUpdateFragmentDialog extends DialogFragment {

	public static VerUpdateFragmentDialog newInstance(
			AppVersionInfo appVersionInfo) {
		VerUpdateFragmentDialog result = new VerUpdateFragmentDialog();
		Bundle bundle = new Bundle();
		bundle.putParcelable("VERSION_INFO", appVersionInfo);
		result.setArguments(bundle);
		return result;
	}

	public VerUpdateFragmentDialog() {
		super();
		this.setRetainInstance(true);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AppVersionInfo appVersionInfo = getArguments().getParcelable(
				"VERSION_INFO");
		Log.d("VerUpdateFragmentDialog",
				"msg:" + appVersionInfo.getDescription());
		this.setCancelable(false);
		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_icon)
				.setTitle(appVersionInfo.getTitle())
				.setMessage(
						appVersionInfo.getDescription().replace("\\n", "\n"))
				.setPositiveButton(R.string.down_release_ver,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								AppVersionInfo info = getArguments()
										.getParcelable("VERSION_INFO");
								if (info.getCompleteFilePath() != null) {
									Intent installIntent = new Intent(
											Intent.ACTION_VIEW);

									installIntent
											.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									installIntent.setDataAndType(
											info.getCompleteFileUri(),
											"application/vnd.android.package-archive");
									getActivity().startActivity(installIntent);
								} else if (info.getUrl() != null
										&& !"".equals(info.getUrl().trim())) {

									DownloadManager.Request down = new DownloadManager.Request(
											Uri.parse(info.getUrl()));

									down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
											| DownloadManager.Request.NETWORK_WIFI);

									down.setAllowedOverRoaming(false);
									down.setVisibleInDownloadsUi(false);
									down.setMimeType("application/vnd.android.package-archive");

									down.setDescription(getResources()
											.getString(R.string.app_name)
											+ getResources()
													.getString(
															R.string.down_app_description)
											+ getResources()
													.getString(
															R.string.down_new_ver_file_description));

									down.setTitle(info.getTitle());

									down.setDestinationInExternalPublicDir(
											Environment.DIRECTORY_DOWNLOADS,
											info.getName() + ".apk");

									((DownloadManager) getActivity()
											.getSystemService(
													Activity.DOWNLOAD_SERVICE))
											.enqueue(down);

									Toast.makeText(
											getActivity()
													.getApplicationContext(),
											R.string.added_download,
											Toast.LENGTH_LONG).show();

								}
							}
						})
				.setNeutralButton(R.string.cancel_down_release_ver, null)
				.setCancelable(false).create();
	}
}
